package de.androidschoice.androids.choice;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.MediaColumns;
import android.util.AttributeSet;
import android.view.View;

import android.widget.ImageView;

import android.widget.Toast;


public class TextSelectionActivity extends Activity {
	private static final int REQUEST_CODE = 10;
	ImageView imageview1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_textselection);
        imageview1 = (ImageView) findViewById(R.id.imageview1);
    }
	
	@Override
	protected void onStart() {
		String id = ((ChoiceApplication)this.getApplication()).getAndroidPostfix();
		if (id.equals("1")){
			imageview1.setImageResource(R.drawable.android1);
		}
		if (id.equals("2")){
			imageview1.setImageResource(R.drawable.android2);
		}
		if (id.equals("3")){
			imageview1.setImageResource(R.drawable.android3);
		}
		if (id.equals("4")){
			imageview1.setImageResource(R.drawable.android4);
		}
		else{
			imageview1.setImageResource(R.drawable.ic_launcher);
		}
		// TODO Auto-generated method stub
		super.onStart();
	}
	
	
	public void onClickSelect(View view) {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("file/*");
		startActivityForResult(intent, REQUEST_CODE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
	    	Uri selectedUri = data.getData();
            String selectedPath = getPath(selectedUri);
            ((ChoiceApplication) getApplication()).setACGPath(selectedPath);
            try {
				((ChoiceApplication) getApplication()).loadACG();
				Intent intent = new Intent(this, TextActivity.class);
		    	this.startActivity(intent);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    } else {
	    	// No file selected
	    }
	}
	
	/**
	 * http://stackoverflow.com/questions/2507898/
	 * how-to-pick-a-image-from-gallery-sd-card-for-my-app-in-android
	 * @param uri
	 * @return
	 */
	public String getPath(Uri uri) {
		String[] projection = { MediaColumns.DATA };
		@SuppressWarnings("deprecation")
		Cursor managedQuery = managedQuery(uri, projection, null, null, null);
		Cursor cursor = managedQuery;
		int column_index = cursor
		        .getColumnIndexOrThrow(MediaColumns.DATA);
		cursor.moveToFirst();

		return cursor.getString(column_index);
	}
	
}
